package excel.XlsCheckboxRead;

import org.apache.poi.hssf.eventusermodel.HSSFListener;
import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
import org.apache.poi.hssf.record.ObjRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.SubRecord;

import java.lang.reflect.Field;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EventExample implements HSSFListener {

    private final static Pattern P = Pattern.compile("\\[sid=0x000A.+?\\[0(\\d),");

    @Override
    public void processRecord(Record record) {

        System.out.println("sid: " + record.getSid() + "----" + record);
        switch (record.getSid()) {

            /*org.apache.poi.hssf.record.CommonObjectDataSubRecord
            org.apache.poi.hssf.record.SubRecord.UnknownSubRecord
            org.apache.poi.hssf.record.SubRecord$UnknownSubRecord [sid=0x000A size=12 : [01, 00, 20, E6, 00, 00, 00, 00, 00, 00, 03, 00]]
            org.apache.poi.hssf.record.SubRecord.UnknownSubRecord
            org.apache.poi.hssf.record.SubRecord.UnknownSubRecord
            org.apache.poi.hssf.record.EndSubRecord*/

            case ObjRecord.sid:
                ObjRecord objRec = (ObjRecord) record;

                List<SubRecord> subRecords = objRec.getSubRecords();
                for (SubRecord subRecord : subRecords) {
                    System.out.println(subRecord.getClass().getCanonicalName());
                    if (subRecord instanceof CommonObjectDataSubRecord) {
                        CommonObjectDataSubRecord dataSubRecord = (CommonObjectDataSubRecord) subRecord;
                        if (dataSubRecord.getObjectType() == CommonObjectDataSubRecord.OBJECT_TYPE_CHECKBOX) {
                            System.out.println("ObjId: "
                                    + dataSubRecord.getObjectId() + "\nDetails: "
                                    + dataSubRecord.toString());
                        }

                    }
                    if (!(subRecord instanceof CommonObjectDataSubRecord)) {
                        Matcher m = P.matcher(subRecord.toString());
                        if (m.find()) {
                            // org.apache.poi.hssf.record.SubRecord$UnknownSubRecord [sid=0x000A size=12 : [01, 00, 20, E6, 00, 00, 00, 00, 00, 00, 03, 00]]
                            System.out.println(subRecord.toString());

                            try {
                                // java 反射获取内部静态类 https://blog.51cto.com/u_16213343/7067111
                                Class outerClass = SubRecord.class;
                                Class innerClass = outerClass.getDeclaredClasses()[0];
                                // 使用内部静态类的Class对象操作类的属性和方法
                                Field field = innerClass.getDeclaredField("_data");
                                field.setAccessible(true);
                                Object value = field.get(subRecord);
                                System.out.println(value);

                                byte[] value1 = (byte[]) value;
                                value1[0] = 0;
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            String checkBit = m.group(1);
                            if (checkBit.length() == 1) {
                                boolean checked = "1".equals(checkBit);
                                ObjectList.getCurrentFlag().add(checked);

                                System.out.println(ObjectList.getCurrentFlag().size() + "--------" + checked + "-----------" + checkBit);
                            }
                        }
                    }
                }
                break;
            default:
                break;
        }
    }

}
